import org.apache.poi.xwpf.usermodel.*;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadWordTemplate {
    public static void main(String[] args) {
        try {
            // 直接读取Word文档
            String filePath = "https://cdtu-competion.oss-cn-chengdu.aliyuncs.com/_%E5%A4%A7%E5%AD%A6%E7%94%9F%E7%AB%9E%E8%B5%9B%E7%94%B3%E8%AF%B7%E8%A1%A820250830_153125.docx";
            FileInputStream fis = new FileInputStream(filePath);
            XWPFDocument document = new XWPFDocument(fis);

            System.out.println("=== Word文档内容分析 ===");
            System.out.println("段落总数: " + document.getParagraphs().size());
            System.out.println("表格总数: " + document.getTables().size());

            // 分析段落
            System.out.println("\n=== 段落内容 ===");
            for (int i = 0; i < document.getParagraphs().size(); i++) {
                XWPFParagraph para = document.getParagraphs().get(i);
                String text = para.getText();
                if (text != null && text.trim().length() > 0) {
                    System.out.println("段落" + i + ": " + text);
                }
            }

            // 分析表格
            System.out.println("\n=== 表格分析 ===");
            for (int tableIdx = 0; tableIdx < document.getTables().size(); tableIdx++) {
                XWPFTable table = document.getTables().get(tableIdx);
                System.out.println("\n表格 " + (tableIdx + 1) + ":");
                System.out.println("  行数: " + table.getRows().size());
                if (table.getRows().size() > 0) {
                    System.out.println("  列数: " + table.getRow(0).getTableCells().size());
                }

                for (int rowIdx = 0; rowIdx < table.getRows().size(); rowIdx++) {
                    XWPFTableRow row = table.getRow(rowIdx);
                    StringBuilder rowText = new StringBuilder();
                    boolean hasPlaceholder = false;
                    boolean isTeacherRelated = false;

                    for (XWPFTableCell cell : row.getTableCells()) {
                        String cellText = cell.getText().trim();
                        rowText.append("[").append(cellText).append("] ");
                        if (cellText.contains("${")) {
                            hasPlaceholder = true;
                        }
                        if (cellText.contains("姓名") || cellText.contains("学历") ||
                            cellText.contains("职称") || cellText.contains("教学单位") ||
                            cellText.contains("备注") || cellText.toLowerCase().contains("teacher")) {
                            isTeacherRelated = true;
                        }
                    }

                    System.out.println("  行" + rowIdx + ": " + rowText.toString());
                    if (hasPlaceholder) {
                        System.out.println("    *** 包含占位符 ***");
                    }
                    if (isTeacherRelated) {
                        System.out.println("    *** 教师相关行 ***");
                    }
                }
            }

            document.close();
            fis.close();

        } catch (IOException e) {
            System.err.println("读取Word文档失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
